// Pulsar Avatar Registration 
// 
// This scripts allows the registration of an avatar into the Pulsar Server 
// to allow connections between users and avatars  
//
// @author Marlon J. Manrique 
// @version 0.1 (13 July 2010)  
//
// License   
// http://creativecommons.org/licenses/GPL/2.0/
//
// $Id

// Pulsar Web Application URL 
string Pulsar_webserver = "http://192.168.1.67/~pulsar/pulsar";

// Webservice request id 
key PulsarWS_requestID;

// Create the url to access the webservice in the server and service specified  
// @param url The Pulsar Server installation url 
// @param serviceName The name of the service to call   
string PulsarWS_createURL(string url, string serviceName) {
	// Add  the index.php and the service name to the url 
	// The Yii Framework add the &ws=1 to the end point of the webservice 
	return url + "/index.php?r=pulsar/" + serviceName + "/ws&ws=1";
}

// Create a request using the SOAP protocol. 
// This method create a envelop with the urn of the service the method to call 
// and the list of parameters. 
//
// The parameter list must contain the elements name, type, value for each parameter
// 
// Example : 
// 	list params = [
//		"key", "string", avatarKey,
//		"name", "string", avatarName
//	];
//
// @param serviceName Name of the webservice to call 
// @param methodName Name of the method to call 
// @param params List of parametes, must contain name, type, value for each parameter 
string PulsarWS_createRequest(string serviceName, string methodName, list params)
{
	// Add the envelope 
	string request = "<soapenv:Envelope "; 
	request = request + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
	request = request + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ";
	request = request + "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "; 
	request = request + "xmlns:urn=\"urn:" + serviceName + "Controllerwsdl\">";
	
	// Empty header 
	request = request + "<soapenv:Header/>";
	
	// The body with the method name 
	request = request + "<soapenv:Body>";  
	request = request + "<urn:" + methodName + " "; 
	request = request + "soapenv:encodingStyle=";
	request = request + "\"http://schemas.xmlsoap.org/soap/encoding/\">";
	
	// Add the parameters 
	// Each parameter is represented by three values 
	integer numParameters = llGetListLength(params)/3;
	
	// Put name, type and value of each parameter
	// [name1,type1,value1,name2,type2,value2]
	integer i;
	for(i=0; i<numParameters; i++) {
		// Get each part 
		string name = llList2String(params, i*3);
		string type = llList2String(params, i*3+1);
		string value = llList2String(params, i*3+2);
		
		// Add each parameter to the request 
		request = request + "<" + name + " xsi:type=\""; 
		request = request + "xsd:" + type + "\">"; 
		request = request + value + "</" + name + ">";
	}
	
	// Close each element of the request 
	request = request + "</urn:" + methodName + ">";
	request = request + "</soapenv:Body>";
	request = request + "</soapenv:Envelope>";
	
	// Return the request 
	return request; 
}

// Call the webservice. 
//
// This method call the webservice with the endpoint especified of the class
// of service name and the method specified, with the parameters 
//
// @param url Endpoint of the webservice 
// @param serviceName Name of the webservice 
// @param MethodName Name of the method to call 
// @param params List of parameters must contain name, type, value for each parameter
PulsarWS_call(string url, string serviceName, string methodName, list params)
{
	// Send the request using HTTP Post and using the soap+xml encode 
	list httpParams = [ 
        HTTP_METHOD, "POST",
        HTTP_MIMETYPE, "application/soap+xml; charset=UTF-8"
    ];
    
    // Create the request and put like the body of the http request 
    string body = PulsarWS_createRequest(serviceName,methodName,params);
    
    // Send the request, save the request id to obtain the response 
    PulsarWS_requestID = llHTTPRequest(url, httpParams, body);
}

// Register the avatar with the key and name specified.  
// 
// This method register the avatar in the web application of Pulsar to allow
// users and avatar association, also allow registration for events or group 
// activities 
// 
// @param avatarKey Metaverse key of the avatar
// @param avatarName Name and lastname of the avatar  
Pulsar_registerAvatar(key avatarKey, string avatarName) {
	
	// Prepare values to call PAvatar.registerAvatar method using SOAP
	string serviceName = "PAvatar";
	string methodName = "registerAvatar";
	
	// Creat the list of parameters for the webservice
	list params = [
		"key", "string", avatarKey,
		"name", "string", avatarName
	];
	
	// Obtain the URL of the end point of the webservice  
	string url = PulsarWS_createURL(Pulsar_webserver,serviceName);	
		 
	// Call the webservice 
    PulsarWS_call(url,serviceName,methodName,params);
}

// Obtain the response send by the webservice 
//
// @param status HTTP status of the request 
// @param body Response send by the webservice 
Pulsar_avatarRegisterReponse(integer status, string body)
{
	// If everythings works fine 
	if(status == 200)
		llSay(0, "Succesful Registration");
	// If any error occurs 
	else
		llSay(0, "Registration Error");
}

// Default State 
// If any avatar touch the prim, the object trys to register the avatar 
// into the Pulsar Server 
default
{
	// Do Nothing (Yet)
	state_entry()
	{
	}
	
	// An avatar touch the prim, 
	// try to register it into the Pulsar server
	touch(integer num_detect)
	{
		// Get key and name of the avatar 
		key avatarKey = llDetectedKey(0);
		string avatarName = llDetectedName(0); 
		
		// Call registration webservice 
		Pulsar_registerAvatar(avatarKey,avatarName); 
	}	
   
   	// Response send by the webservice 
	http_response(key request_id, integer status, list metadata, string body)
	{
		// Check if the request is the webservice 
		if(request_id == PulsarWS_requestID)
			Pulsar_avatarRegisterReponse(status,body);
	}
}